package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.node.TreeBack;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.system.entity.Classification;
import cn.stylefeng.guns.modular.system.entity.ShiWarehouse;
import cn.stylefeng.guns.modular.system.model.params.ShiWarehouseParam;
import cn.stylefeng.guns.modular.system.service.ShiWarehouseService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * 仓库表控制器
 *
 * @author 小飞
 * @Date 2019-06-22 11:37:16
 */
@Controller
@RequestMapping("/shiWarehouse")
public class WarehouseController extends BaseController {

    private String PREFIX = "/shiWarehouse";

    @Autowired
    private ShiWarehouseService shiWarehouseService;

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/shiWarehouse.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("/add")
    public String add() {

        return PREFIX + "/shiWarehouse_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/shiWarehouse_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ShiWarehouseParam shiWarehouseParam) {
        Long userId = ShiroKit.getUserNotNull().getId();
        //查询部门id
        User user = userService.getById(userId);
        this.shiWarehouseService.add(shiWarehouseParam, user.getDeptId());
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ShiWarehouseParam shiWarehouseParam) {
        this.shiWarehouseService.update(shiWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ShiWarehouseParam shiWarehouseParam) {
        this.shiWarehouseService.delete(shiWarehouseParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ShiWarehouseParam shiWarehouseParam) {
        ShiWarehouse detail = this.shiWarehouseService.getById(shiWarehouseParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-22
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ShiWarehouseParam shiWarehouseParam) {
        //获取用户id
        Long userId = ShiroKit.getUserNotNull().getId();
        List<Long> idList = new ArrayList<>();
        //查询部门id
        User user = userService.getById(userId);
        //查询部门信息
        Dept dept = deptService.getById(user.getDeptId());
        if (dept != null) {
            idList.add(dept.getDeptId());
            QueryWrapper<Dept> wrapper = new QueryWrapper<>();
            //判断是否还有下级部门
            wrapper.lambda()
                    .eq(Dept::getPid, dept.getDeptId());
            List<Dept> deps = deptService.list(wrapper);
            if (deps.size() > 0) {
                for (Dept d : deps) {
                    idList.add(d.getDeptId());
                    //查询是否有下级部门
                    wrapper.lambda()
                            .eq(Dept::getPid, d.getDeptId());
                    List<Dept> dList = deptService.list(wrapper);
                    if (dList.size() > 0) {
                        for (Dept de : dList) {
                            idList.add(de.getDeptId());
                        }
                    }
                }
            }
        }
        return this.shiWarehouseService.findPageBySpec(shiWarehouseParam, idList);
    }

    @RequestMapping("/warhouseList")
    public String unitsByProductId() {
        super.setAttr("wareList", shiWarehouseService.list());
        return PREFIX + "/warhouseList.html";
    }

    /**
     * 获取树结构
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTree")
    public List<ZTreeNode> getTree() {

        List<ZTreeNode> treeNodes = this.shiWarehouseService.tree();
        treeNodes.add(ZTreeNode.createParent());

        return treeNodes;
    }

    @ResponseBody
    @RequestMapping(value = "/search")
    public TreeBack search(String name) throws UnsupportedEncodingException {
        name = URLDecoder.decode(name, "UTF-8");
        QueryWrapper<ShiWarehouse> areaQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(name)) {
            areaQueryWrapper.lambda().eq(ShiWarehouse::getIsDelete, 0);
            return new TreeBack(0, "ok", shiWarehouseService.list(areaQueryWrapper));
        } else {
            areaQueryWrapper.like("w_name", name);
            return new TreeBack(0, "ok", shiWarehouseService.list(areaQueryWrapper));
        }
    }
}


